package com.assignment.health.ui.profile;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

import com.assignment.health.ChangePasswordActivity;
import com.assignment.health.LoginActivity;
import com.assignment.health.MainActivity;
import com.assignment.health.R;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.net.http.NetClient;
import com.user.UserInformation;

import java.util.ArrayList;
import java.util.List;


public class ProfileFragment extends Fragment {

    class MessageType {
        public static final int getInforReturn = 0x01;
        public static final int getLogoutReturn = 0x02;
    }

    private String mParam1;
    private String mParam2;
    private ArrayList<String> userInforStr;
    private ArrayAdapter<String> adapter;


    private ListView listView;
    private Button buttonLogout;
    private Button buttonChangePassword;

    private Handler handler;

    @SuppressLint("HandlerLeak")
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.fragment_profile, container, false);

        listView = (ListView) rootView.findViewById(R.id.listview_person_infor);
        buttonLogout = (Button) rootView.findViewById(R.id.button_logout);
        buttonChangePassword = (Button) rootView.findViewById(R.id.button_change_password);

        new Thread(new Runnable() {
            @Override
            public void run() {
                JsonObject json = NetClient.GetInfor(UserInformation.token);
                handler.sendMessage(handler.obtainMessage(MessageType.getInforReturn, json));
            }
        }).start();

        userInforStr = new ArrayList<String>();
        adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, userInforStr);
        listView.setAdapter(adapter);

        buttonLogout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                buttonLogout.setEnabled(false);
                buttonChangePassword.setEnabled(false);
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        JsonObject json = NetClient.GetInfor(UserInformation.token);
                        handler.sendMessage(handler.obtainMessage(MessageType.getLogoutReturn, json));
                    }
                }).start();
            }
        });

        buttonChangePassword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent MainIntent = new Intent(getActivity(), ChangePasswordActivity.class);
                startActivity(MainIntent);
            }
        });

        handler = new Handler() {
            public void handleMessage(Message msg) {
                if(msg.what == MessageType.getInforReturn) {
                    JsonObject json = (JsonObject) msg.obj;
                    if(json.get("status").getAsString().contentEquals("OK")) {
                        String str = json.get("data").toString();
                        json = new JsonParser().parse(str).getAsJsonObject();
                        userInforStr.add(String.format("%s: %s", "id", json.get("id").getAsString()));
                        userInforStr.add(String.format("%s: %s", "name", json.get("real_name").getAsString()));
                        userInforStr.add(String.format("%s: %s", "gender", json.get("gender").getAsString()));
                        userInforStr.add(String.format("%s: %s", "age", json.get("age").getAsString()));
                        userInforStr.add(String.format("%s: %s", "telephone", json.get("tel").getAsString()));
                    }
                    adapter.notifyDataSetChanged();
                }
                else if(msg.what == MessageType.getLogoutReturn) {
                    JsonObject json = (JsonObject) msg.obj;
                    if(json.get("status").getAsString().contentEquals("OK")) {
                        getActivity().finish();
                    }
                    else {
                        buttonLogout.setEnabled(true);
                        buttonChangePassword.setEnabled(true);
                    }
                }
            }
        };

        return /*inflater.inflate(R.layout.fragment_profile, container, false)*/rootView;
    }
}